/* 
 * File:   ReferenceTerminal.h
 * Author: cpp
 *
 * Created on May 4, 2010, 3:49 AM
 */

#ifndef _REFERENCETERMINAL_H
#define	_REFERENCETERMINAL_H
#include "Terminal.h"

using namespace std;

class ReferenceTerminal : public Terminal {
public:
    ReferenceTerminal();
    ReferenceTerminal(const ReferenceTerminal& orig);
    virtual ~ReferenceTerminal();
    void setId(const string& id);
    void setTable(const string& table);
    void setAttrib(const string& attrib);

    const string& getTable() const {
        return table;
    }

    const string& getAttrib() const {
        return attrib;
    }

    string getSID();
    void buildFile(string path);
    void buildSpec(string path);

private:
    string table;
    string attrib;

};

ReferenceTerminal::ReferenceTerminal() {
}

ReferenceTerminal::ReferenceTerminal(const ReferenceTerminal& orig) {
    id = orig.id;
    table = orig.table;
    attrib = orig.attrib;
    value = orig.value;

}

ReferenceTerminal::~ReferenceTerminal() {
}

void ReferenceTerminal::setId(const string& id) {
    if ((id.substr(0, 3) == "(R:") && (id[id.size() - 1] == ')'))//and
    {
        this->id = id;
    }
}

void ReferenceTerminal::setTable(const string& tbl) {
    this->table = tbl;
}

void ReferenceTerminal::setAttrib(const string& attrib) {
    this->attrib = attrib;
}

void ReferenceTerminal::buildFile(string filename) {

    ofstream myfile(filename.c_str(), ios::app);
    myfile << ".RT" << "\n";
    myfile << "ID: " << this->getId() << "\n";
    myfile << "TN: " << this->getTable() << "\n"; //tablename
    myfile << "SA: " << this->getAttrib() << "\n";
    myfile << ".END_RT" << "\n";
    myfile.close();
}

//Get the DRT id for accent specification file

void ReferenceTerminal::buildSpec(string filename) {

    string r, m;
    bool numswitch;
    numswitch = false;
    string SID;
    r = this->getId();
    if (r[r.size()-2] == '#')
    {
        numswitch = true;
    }

    SID = this->getSID();
    cout << "EXPAND: " << SID << endl;
    ofstream specfile(filename.c_str(), ios::app);

    specfile << SID << ":\n";
//    specfile << "{RTswitchON(); } ";
    if (numswitch){
        specfile << "NUMBER {printNRT(\"" << this->getId() << " = \");";
    }else{
        specfile << "COMMENT {printRT(\"" << this->getId() << " = \");";
    }

  //  specfile << "RTswitchOFF();} \n;\n";
    specfile << "}\n;\n";
    specfile.close();
}

string ReferenceTerminal::getSID() {
    //TODO: StringStream
    string sid;
    stringstream ssid;
    ssid << "RT";
    sid = this->getId();
    sid = sid.substr(3, sid.size() - 4);
    ssid << sid << endl;
    sid = ssid.str();
    if (sid[sid.size()-2] == '#')
    {
        sid = sid.substr(0,sid.size()-2);
    }
    return sid;
}


#endif	/* _REFERENCETERMINAL_H */

